<?php

class Taxi_Model_Estat extends Zend_Db_Table_Abstract
{
    protected $_name = 'estat';
    protected $_primary = 'mail';
    
    public function consultar_estat($mail)
    {
        $where = $this->_db->select()
                ->from(array ('s'=>$this->_name),
                       array ('id_solicitud','diferida'))
                ->where('mail = ?', $mail);
        
        $result = $where->query()->fetchAll();
        
        if (sizeof($result)== 0)
        {
            return -1;
        }else
        {
            return $result;
        }
     
    }
    
    public function insert_position($mail,$posX,$posY)
    {
        $data = array(
            'posX'  =>  $posX,
            'posY'  =>  $posY
            );
        
        $where = $this->getAdapter()->quoteInto('mail = ?', $mail);
        
        $this->update($data, $where);
        
    }
   
    public function login($mail){
        
        $select = $this->_db->select()
                    ->from($this->_name)
                    ->where('mail = ?', $mail);
        
        $result = $select->query()->fetchAll();
        
        if (sizeof($result) == 0){
            
            $data = array(
            'mail'          =>  $mail,
            'logged'        =>  '1',
            'posX'          =>  '0',
            'posY'          =>  '0',
            'id_solicitud'  =>  '-1'
            );
        
            $this->insert($data);
            
        }else {
            $data = array('logged'  =>  '1');
            $where = $this->getAdapter()->quoteInto('mail = ?', $mail);
        
            $this->update($data, $where);
        }
    }
    
    public function logout($mail){
        
        $data = array('logged'  =>  '0');
        $where = $this->getAdapter()->quoteInto('mail = ?', $mail);
        
        $update = $this->update($data, $where);
        
        if ($update == 0)
        {
            return -1;
        }else
        {
            return 0;
        } 
    }
    
    public function reject_solicitud($mail){
        $data = array('id_solicitud'  =>  '-1',
                      'diferida'      =>  '0');
        $where = $this->getAdapter()->quoteInto('mail = ?', $mail);
        
        $update = $this->update($data, $where);
        
        if ($update == 0)
        {
            return -4;
        }else
        {
            return 0;
        }      
    }
    
    public function peticio($mail_taxi, $id_sol,$diferida){
        
        $data = array('id_solicitud' => $id_sol,
                      'diferida'     => $diferida);
        $where = $this->getAdapter()->quoteInto('mail = ?', $mail_taxi);
        
        $update = $this->update($data, $where);
        
        if ($update == 0)
        {
            return -6;
        }else
        {
            return 0;
        }
    }

    public function pregunta_al_mes_proper($posX, $posY,$mail){
        
        $select = $this->_db->select()
                    ->from($this->_name)
                    ->where('logged = 1 and id_solicitud = -1')
                    ->where('not mail = ?',$mail);
        
        $result = $select->query()->fetchAll();
        
        $d = -1;
        $j = -1;
        for($i = 0; $i < sizeof($result); $i++){
            $d_aux = $this->dist($posX, $posY, $result[$i]['posx'], $result[$i]['posy']);
            if($d == -1 || $d_aux < $d){
                $d = $d_aux;
                $j = $result[$i]['mail'];
            }
        }
        
        if (is_null($j)){
            return -7;
        }else {
            return $j;
        }
    }

  
    private function dist($aX, $aY, $bX, $bY){
        return sqrt((($aX-$bX)*($aX-$bX))+(($aY-$bY)*($aY-$bY)));
    }
}

